// pages/network/login.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    flag: 0,
    loadingHidden: true,
    student_id: '',
    campus_password: ''
  },

  onLoad: function (options) {
    wx.showShareMenu({
      showShareItems: ['qq', 'qzone', 'wechatFriends', 'wechatMoment']
    });
    wx.showToast({
      title: '此服务需要连接校园网',
      icon: 'none',
      duration: 2000
    });
  },

  onBindStudent_id: function (e) {
    var that = this;
    that.setData({
      student_id: e.detail.value
    })
  },

  onBindPassword: function (e) {
    var that = this;
    that.setData({
      'campus_password': e.detail.value
    })
  },

  onShow: function () {

  },

  bind_to_network: function () {
    var that = this;
    if (that.data.student_id == '') {
      wx.showModal({
        title: '提示',
        content: '请输入学号',
        showCancel: false,
      })
    } else if (that.data.campus_password == '') {
      wx.showModal({
        title: '提示',
        content: '请输入密码',
        showCancel: false,
      })
    } else {
      that.setData({
        loadingHidden: false
      });
      wx.setStorageSync("student_id", that.data.student_id)
      wx.request({
        url: app.globalData.netUrl + 'api/net/login/',
        method: 'post',
        header: {
          "Content-type": "application/x-www-form-urlencoded"  //请求一定记得header 
        },
        data: {
          'username': that.data.student_id,
          'password': that.data.campus_password
        },
        success: res => {
          if (res.statusCode == 7) {
            that.setData({
            loadingHidden: true
          });
          wx.showModal({
            title: '提示',
            content: '请将手机连接校园网后重试!',
            showCancel: false
          })
          } else {
            that.setData({
              loadingHidden: true
            });
            var error = res.data.error;
            if (error == -1) {
              wx.showModal({
                title: '提示',
                content: '密码错误，请重新输入',
                showCancel: false,
              })
              that.setData({
                loadingHidden: true
              });
            } else if (error == 500) {
              wx.showModal({
                title: '提示',
                content: '网络超时,请重试',
                showCancel: false,
              })
              that.setData({
                loadingHidden: true
              });

            } else {
              that.setData({
                loadingHidden: true

              });
              wx.setStorageSync("cookie", res.data.data.cookie);
              wx.showModal({
                title: '提示',
                content: '登录成功',
                showCancel: false,
                success: function (res) {
                  wx.navigateTo({
                    url: '/pages/network/shipment'
                  })
                }
              })
            }
          }
        },
        fail: function (res) {
          that.setData({
            loadingHidden: true
          });
          wx.showModal({
            title: '提示',
            content: '请将手机连接校园网后重试!',
            showCancel: false
          })
        }
      })
    }
  }

})